package gonz.data.swim;

import gonz.common.PriceAndArticleParser;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class Bottom {
    public static final Logger log = Logger.getLogger(Bottom.class);

    public static final String[] PRICE_ARTICLE_XPATHS = new String[]{
            "//div[@id='related-product-1']/div/p[5]",   //
            "//div[@id='related-product-1']/div/p[4]",
            "//div[@id='related-product-1']/div/p[3]",     //
            "//div[@id='related-product-1']/div/p[2]"};

    public static final String UNKNOWN_PRICES_AND_ARTICLE_SYMBOL = "";

    private WebDriver webDriver;

    private String solidsPrice;
    private String printsPrice;
    private String specialPrice;
    private String origPrice;
    private String salePrice;
    private String price;

    private String article;
    private Swim swim;

    public Bottom(WebDriver webDriver, Swim swim) {
        this.webDriver = webDriver;
        this.swim = swim;
        String pricesAndArticle = grabPricesAndArticle();
        this.solidsPrice = PriceAndArticleParser.parsePrice(pricesAndArticle, PriceAndArticleParser.PRICE_SOLIDS_ID);
        this.printsPrice = PriceAndArticleParser.parsePrice(pricesAndArticle, PriceAndArticleParser.PRICE_PRINTS_ID);
        this.specialPrice = PriceAndArticleParser.parsePrice(pricesAndArticle, PriceAndArticleParser.PRICE_SPECIAL_ID);
        this.origPrice = PriceAndArticleParser.parsePrice(pricesAndArticle, PriceAndArticleParser.PRICE_ORIG_ID);
        this.salePrice = PriceAndArticleParser.parsePrice(pricesAndArticle, PriceAndArticleParser.PRICE_SALE_ID);
        this.price = PriceAndArticleParser.parsePrice(pricesAndArticle);
        this.article = PriceAndArticleParser.parseArticle(pricesAndArticle);
        if (!isPriceExists()) {
            this.price = PriceAndArticleParser.parseAllPrices(pricesAndArticle);
            if (this.price.equalsIgnoreCase(PriceAndArticleParser.UNKNOWN_SYMBOL))
                log.error("Price for Bottom not found. " + webDriver.getCurrentUrl());
        }
    }

    public String getSolidsPrice() {
        return solidsPrice;
    }

    public String getPrintsPrice() {
        return printsPrice;
    }

    public String getSpecialPrice() {
        return specialPrice;
    }

    public String getOrigPrice() {
        return origPrice;
    }

    public String getSalePrice() {
        return salePrice;
    }

    public String getPrice() {
        return price;
    }

    public String getArticle() {
        return article;
    }

    public Swim getSwim() {
        return swim;
    }

    private String grabPricesAndArticle() {

        for (int i = 0; i < PRICE_ARTICLE_XPATHS.length; i++) {
            try {
                WebElement element = webDriver.findElement(By.xpath(PRICE_ARTICLE_XPATHS[i]));
                if (element == null || element.getText().isEmpty())
                    log.debug("Can't find Bottom price and article element: " + PRICE_ARTICLE_XPATHS[i] + " swim link: " + getSwim().getLink());
                else {
                    if (PriceAndArticleParser.isArticleExist(element.getText())) {
                        log.debug("TwoPiecesSwim Bottom prices and article has been grabbed: " + PRICE_ARTICLE_XPATHS[i] + " swim link: " + getSwim().getLink());
                        return element.getText();
                    }
                }
            } catch (NoSuchElementException e) {
                log.debug("Can't find Bottom price and article element: " + PRICE_ARTICLE_XPATHS[i] + " swim link: " + getSwim().getLink());
            }

        }
        log.error("Can't grab Bottom prices and article. " + webDriver.getCurrentUrl());
        return UNKNOWN_PRICES_AND_ARTICLE_SYMBOL;
    }

    private boolean isPriceExists() {
        return !(PriceAndArticleParser.UNKNOWN_SYMBOL.equalsIgnoreCase(getPrice())
                && PriceAndArticleParser.UNKNOWN_SYMBOL.equalsIgnoreCase(getPrintsPrice())
                && PriceAndArticleParser.UNKNOWN_SYMBOL.equalsIgnoreCase(getSolidsPrice())
                && PriceAndArticleParser.UNKNOWN_SYMBOL.equalsIgnoreCase(getSalePrice())
                && PriceAndArticleParser.UNKNOWN_SYMBOL.equalsIgnoreCase(getOrigPrice())
//                && PriceAndArticleParser.UNKNOWN_SYMBOL.equalsIgnoreCase(getClearancePrice())
                && PriceAndArticleParser.UNKNOWN_SYMBOL.equalsIgnoreCase(getSpecialPrice()));
    }
}
